<?php

if(empty($buid))

msg($config['weburl']."/login.php?forward=".urlencode("$config[weburl]/?m=product&s=shop_order"));
else
{
	if(!empty($buid))
	{
		if($_POST['act'] != 'order'){
			include_once("$config[webroot]/module/member/includes/plugin_orderadder_class.php");
			include_once("$config[webroot]/module/logistics/includes/logistics_class.php");
			$orderadder=new orderadder();$logistics = new logistics();
			$param = $_POST;


			if(empty($param['property'])){
				echo "<script type='text/javascript'> alert('请选择票品');window.history.back();window.location.reload();</script>";
				return false;
			}
			if($param['num'] <= 0 ){
				echo "<script type='text/javascript'> alert('数量异常');window.history.back();window.location.reload();</script>";
				return false;
			}
			if($param['delivery_way'] != 'pickup' ){

				if(empty($param['ship_name'])||empty($param['provinceid']) || empty($param['cityid']) || empty($param['areaid']) ||empty($param['ship_mobile']) ||empty($param['ship_addr'])){

				echo "<script type='text/javascript'> alert('请填写地址');window.history.back();window.location.reload();</script>";
				return false;
			}

			} else {

				if(empty($param['pick_name'])||empty($param['pick_mobile']) || empty($param['pick_provinceid']) || empty($param['pick_addr']) ){
				echo "<script type='text/javascript'> alert('请填写取票地址');window.history.back();window.location.reload();</script>";
				return false;

			}
		}
			
			if(empty($param['delivery_way'])){
				echo "<script type='text/javascript'> alert('请选择配送方式');window.history.back();window.location.reload();</script>";
				return false;
			}

			$explode = explode(",",$param['property']);
			$sql = "select s.*,p.pname from ".SETMEAL." s left join ".PRO." p on s.pid = p.id where s.pid = '{$param['pid']}' and s.performance_start_time = '{$explode[0]}' and s.price = '{$explode[1]}' ";
			$db->query($sql);
			$product_info = $db->fetchRow();
			$total = round($product_info['price'] * $param['num'],2);
			if($product_info['stock'] <  $param['num']){
				echo "<script type='text/javascript'> alert('库存不足');window.history.back();window.location.reload();</script>";
				return false;
			}

			$db->query("select * from ".MEMBERADDR." where name='{$param['ship_name']}' and provinceid='{$param['provinceid']}' and cityid='{$param['cityid']}' and areaid='{$param['areaid']}' and address='{$param['ship_addr']}' and mobile='{$param['ship_mobile']}' ");
			$is_address_exits = $db->fetchRow();
			if(empty($is_address_exits)){
				//新增地址
				$address_sql = "insert into ".MEMBERADDR." (`userid`,`name`,`provinceid`,`cityid`,`areaid`,`address`,`mobile`) VALUES
 				('$buid','{$param['ship_name']}','{$param['provinceid']}','{$param['cityid']}','{$param['areaid']}','{$param['ship_addr']}','{$param['ship_mobile']}')";
				$db->query($address_sql);
			}

			//实名验证
			$cc_sql = "select * from ".CARDCHECK." where uid = '$buid' and real_card_type = '{$param['real_card_type']}' and real_card_no = '{$param['real_card_no']}' ";
			$db->query($cc_sql);
			$card_check = $db->fetchRow();
			if(empty($card_check)){
				$add_time = time();
				$cci_sql = "insert into ".CARDCHECK." (`uid`,`real_user_name`,`real_card_type`,`real_card_no`,`add_at`) VALUES
 				('$buid','{$param['real_user_name']}','{$param['real_card_type']}','{$param['real_card_no']}','$add_time')";
				$db->query($cci_sql);
			}


			//选择配送方式
			$shipping = [];
			if($param['delivery_way'] == 'express'){
				$ship_sql = "select l.*,c.default_price from ".LGSTEMP." l left join ".LGSTEMPCON." c on l.id = c.temp_id where l.type=0";
				$db->query($ship_sql);
				$shipping = $db->fetchRow();
			//	$freight = $shipping['default_price'];
				if($param['provinceid'] == $product_info['province'] && $param['cityid'] ==$product_info['city'] ){
					$freight = $shipping['default_price']; //默认 同城
				} elseif($param['provinceid'] == $product_info['province'] && $param['cityid'] !=$product_info['city']){
					$freight = 13; //省内
				} elseif($param['provinceid'] != $product_info['province']){
					$freight = 22; //省外
				} elseif(in_array(['32,33,34'],$param['provinceid'])){
					$freight = 92; // 港澳台
				} else {
					$freight = $shipping['default_price']; //默认 同城

				}

			} else {
				$freight = 0;
				$shipping['title'] = "上门自取";
				$shipping['id'] = 2;
			}

			if($total <=0){
				echo "<script type='text/javascript'> alert('支付金额异常');window.history.back();window.location.reload();</script>";
				return false;
			}

			$cost = $total;
			$user_agent = $_SERVER['HTTP_USER_AGENT'];
			//支付方式
			if(strpos($user_agent, 'MicroMessenger') === false){
				$payment_types = "wapalipay";
			} else {
				$payment_types = "wechatpay";
			}
			$sql = "select payment_id,payment_name from ".PAYMENT." where payment_type='$payment_types' ";
			$db->query($sql);
			$pay = $db->fetchRow();
			//创建订单
			$order_id=date("Ymdhis").rand(0,9);//订单号
			$pname=!empty($product_info['pname']) ? $product_info['pname'] : $config['company'];//此次购物的产品名总称


			/***生成订单****/
			$sql = "INSERT INTO ".ORDER."(member_id,order_id,create_time,payment_id,payment_name,ship_name,ship_addr,ship_mobile,cost,freight,provinceid,cityid,
			areaid,shipping_id,shipping_name,point,invoice_type,invoice_company,invoice_addr,pick_name,pick_mobile,pick_provinceid,pick_addr,
			real_user_name,real_card_type,real_card_no) VALUES
				($buid,'$order_id','".time()."','{$pay['payment_id']}','{$pay['payment_name']}','{$param['ship_name']}','{$param['ship_addr']}','{$param['ship_mobile']}',
				'$cost','$freight','{$param['provinceid']}','{$param['cityid']}','{$param['areaid']}','{$shipping['id']}','{$shipping['title']}','{$product_info['point']}',
				'{$param['invoice_type']}','{$param['invoice_company']}','{$param['invoice_addr']}','{$param['pick_name']}','{$param['pick_mobile']}',
				'{$param['pick_provinceid']}','{$param['pick_addr']}','{$param['real_user_name']}','{$param['real_card_type']}','{$param['real_card_no']}')";
			$flag=$db->query($sql);

			//--产品信息
			$sql = "INSERT INTO ".ORPRO." (`order_id`,`buyer_id`,`pid`,`pcatid`,`name`,`pic`,`price`,`num`,`time`,`product_id`,`per_date`,`spot_id`) VALUES
			($order_id,$buid,$product_info[pid],'$product_info[catid]','$product_info[pname]','".$product_info['pic']."','".$product_info['price']."','".$param['num']."','".time()."','{$product_info['id']}','{$product_info['performance_start_time']}',
			'{$product_info['spot']}')";
			$db->query($sql);
			$pname=$pname.'-'.$product_info['pname'];


			include_once("$config[webroot]/module/product/includes/plugin_order_class.php");
			$order=new order();
			$order->order_log($order_id,'订单创建','创建');
			$order->add_order_delivery($order_id,$shipping,$consignee);
			$order->add_order_payment($order_id,($cost+$freight),$pay);

			include_once("$config[webroot]/module/payment/lang/$config[language].php");
			$post['type']=1;//直接到账
			$post['action']='add';//
			$post['buyer_email']=$buid;
			$post['seller_email']='admin@systerm.com';
			$post['order_id']=$order_id;//外部订单号
			$post['price']=$cost + $freight;;//订单总价，单价元
			$post['return_url']='main.php?m=product&s=admin_orderdetail&id='.$order_id.'&type=buy';//返回地址
			$post['notify_url']='main.php?m=product&s=admin_orderdetail&id='.$order_id.'&type=buy';//异步返回地址。
			$post['note']=$pname;
			pay_get_url($post);//跳转至订单生成页面
			if($re<0)
			{
				if($re==-2)
					msg('main.php?m=payment&s=admin_info','您的支付账户还没有开通');
				if($re==-1)
					msg("$config[weburl]/?m=product&s=cart",'卖家没有开通支付功能，暂不能购买');
			}







		} else {
			//=============================提交订单
			foreach($_POST as $key => $val){
				if(!is_array($val)){
					$param[$key] = htmlspecialchars(stripslashes($val));
				} else {
					$param[$key] = $val;
				}
			}


			if(empty($param['ship_name'])||empty($param['provinceid']) || empty($param['cityid']) || empty($param['areaid']) ||empty($param['ship_mobile']) ||empty($param['ship_addr'])){
				echo "<script type='text/javascript'> alert('请填写地址');window.history.back();</script>";
				return false;
			}
			if(empty($param['delivery_way'])){
				echo "<script type='text/javascript'> alert('请选择配送方式');window.history.back();</script>";
				return false;
			}
			if(empty($param['paytype'])){
				echo "<script type='text/javascript'> alert('请选择支付方式');window.history.back();</script>";
				return false;
			}
			if(empty($param['product_id']) || empty($param['num'])){
				echo "<script type='text/javascript'> alert('请选择购买的票品');window.location.href='".$config['weburl']."';</script>";
				return false;
			}
			if($param['num'] <= 0 ){
				echo "<script type='text/javascript'> alert('选择票品数量异常');window.history.back();</script>";
				return false;
			}

			$product_total_price = 0; //产品总价格
			$p_sql = "select s.*,p.pname,p.point from ".SETMEAL." s left join ".PRO." p on s.pid = p.id where s.id = '{$param['product_id']}' ";
			$db->query($p_sql);
			$product_info = $db->fetchRow();
			$product_total_price = $product_info['price'] * $param['num'];
			$pw_date = explode(',',$product_info['setmeal']);
			$product_info['per_date'] = strtotime($pw_date[0]);




			$db->query("select * from ".MEMBERADDR." where name='{$param['ship_name']}' and provinceid='{$param['provinceid']}' and cityid='{$param['cityid']}' and areaid='{$param['areaid']}' and address='{$param['ship_addr']}' and mobile='{$param['ship_mobile']}' ");
			$is_address_exits = $db->fetchRow();
			if(empty($is_address_exits)){
				//新增地址
				$address_sql = "insert into ".MEMBERADDR." (`userid`,`name`,`provinceid`,`cityid`,`areaid`,`address`,`mobile`) VALUES
 				('$buid','{$param['ship_name']}','{$param['provinceid']}','{$param['cityid']}','{$param['areaid']}','{$param['ship_addr']}','{$param['ship_mobile']}')";
				$db->query($address_sql);
			}
			//选择配送方式
			$shipping = [];
			if($param['delivery_way'] == 'express'){
				$ship_sql = "select l.*,c.default_price from ".LGSTEMP." l left join ".LGSTEMPCON." c on l.id = c.temp_id where l.type=0";
				$db->query($ship_sql);
				$shipping = $db->fetchRow();

				if($param['provinceid'] == $product_info['province'] && $param['cityid'] ==$product_info['city'] ){
					$freight = $shipping['default_price']; //默认 同城
				} elseif($param['provinceid'] == $product_info['province'] && $param['cityid'] !=$product_info['city']){
					$freight = 13; //省内
				} elseif($param['provinceid'] != $product_info['province']){
					$freight = 22; //省外
				} elseif(in_array(['32,33,34'],$param['provinceid'])){
					$freight = 92; // 港澳台
				} else {
					$freight = $shipping['default_price']; //默认 同城
				}


			} else {
				$freight = 0;
			}

			$cost = $product_total_price;

			if($cost <=0){
				echo "<script type='text/javascript'> alert('支付金额异常');window.history.back();</script>";
				return false;
			}

			//支付方式
			$sql = "select payment_id,payment_name from ".PAYMENT." where payment_type='{$param['paytype']}' ";
			$db->query($sql);
			$pay = $db->fetchRow();
			//创建订单
			$order_id=date("Ymdhis").rand(0,9);//订单号
			$pname=!empty($product_info['pname']) ? $product_info['pname'] : $config['company'];//此次购物的产品名总称

			/***生成订单****/
			$sql = "INSERT INTO ".ORDER."(member_id,order_id,create_time,payment_id,payment_name,ship_name,ship_addr,ship_mobile,cost,freight,provinceid,cityid,areaid,shipping_id,shipping_name,point) VALUES
				($buid,'$order_id','".time()."','{$pay['payment_id']}','{$pay['payment_name']}','{$param['ship_name']}','{$param['ship_addr']}','{$param['ship_mobile']}','$cost','$freight','{$param['provinceid']}','{$param['cityid']}',
				'{$param['areaid']}','{$shipping['id']}','{$shipping['title']}','{$product_info['point']}')";
			$flag=$db->query($sql);

			//--产品信息
			$sql = "INSERT INTO ".ORPRO." (`order_id`,`buyer_id`,`pid`,`pcatid`,`name`,`pic`,`price`,`num`,`time`,`product_id`,`per_date`) VALUES
			($order_id,$buid,$product_info[pid],'$product_info[catid]','$product_info[pname]','".$product_info['pic']."','".$product_info['price']."','".$param['num']."','".time()."','{$product_info['id']}','{$product_info['per_date']}')";
			$db->query($sql);
			$pname=$pname.'-'.$product_info['pname'];


			include_once("$config[webroot]/module/product/includes/plugin_order_class.php");
			$order=new order();
			$order->order_log($order_id,'订单创建','创建');
			$order->add_order_delivery($order_id,$shipping,$consignee);
			$order->add_order_payment($order_id,($cost+$freight),$pay);

			include_once("$config[webroot]/module/payment/lang/$config[language].php");
			$post['type']=1;//直接到账
			$post['action']='add';//
			$post['buyer_email']=$buid;
			$post['seller_email']='admin@systerm.com';
			$post['order_id']=$order_id;//外部订单号
			$post['price']=$cost + $freight;;//订单总价，单价元
			$post['return_url']='main.php?m=product&s=admin_orderdetail&id='.$order_id.'&type=buy';//返回地址
			$post['notify_url']='main.php?m=product&s=admin_orderdetail&id='.$order_id.'&type=buy';//异步返回地址。
			$post['note']=$pname;
			pay_get_url($post);//跳转至订单生成页面
			if($re<0)
			{
				if($re==-2)
					msg('main.php?m=payment&s=admin_info','您的支付账户还没有开通');
				if($re==-1)
					msg("$config[weburl]/?m=product&s=cart",'卖家没有开通支付功能，暂不能购买');
			}

		}



	}
}
//=================================================
$tpl->assign("config",$config);
$tpl->assign("current","product");
include_once("footer.php");

$out=tplfetch("shop_wap_order.htm",$flag,true);

?>